<template>
  <div class="setdialog">
    <el-dialog title="新增员工" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off" />
        </el-form-item>
        <el-form-item label="入职时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="聘用形式" :label-width="formLabelWidth">
          <el-select
            v-model="form.formOfEmployment"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" :label-width="formLabelWidth">
          <el-input v-model="form.workNumber" autocomplete="off" />
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-input
            v-model="form.departmentName"
            autocomplete="off"
            @focus="isShowtree = true"
          />
          <el-tree
            v-show="isShowtree"
            :data="data"
            :props="defaultProps"
            default-expand-all
            icon-class="false"
            @node-click="handleNodeClick"
          />
        </el-form-item>
        <el-form-item label="转正时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { formatData } from '@/utils'
import EMPLOYEECON from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  name: 'Setdialog',
  data() {
    return {
      formLabelWidth: '120px',
      dialogFormVisible: false,
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      isShowtree: false,
      data: [
        {
          label: '一级 1',
          children: [
            {
              label: '二级 1-1',
              children: [
                {
                  label: '三级 1-1-1'
                }
              ]
            }
          ]
        },
        {
          label: '一级 2',
          children: [
            {
              label: '二级 2-1',
              children: [
                {
                  label: '三级 2-1-1'
                }
              ]
            },
            {
              label: '二级 2-2',
              children: [
                {
                  label: '三级 2-2-1'
                }
              ]
            }
          ]
        },
        {
          label: '一级 3',
          children: [
            {
              label: '二级 3-1',
              children: [
                {
                  label: '三级 3-1-1'
                }
              ]
            },
            {
              label: '二级 3-2',
              children: [
                {
                  label: '三级 3-2-1'
                }
              ]
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      hireType: EMPLOYEECON.hireType
    }
  },
  async created() {
    const res = await getDepartments()
    // console.log(res)
    this.data = formatData(res.depts, '')
  },
  methods: {
    handleNodeClick(data) {
      // console.log(data)
      this.form.departmentName = data.name
      this.isShowtree = false
    },
    async submitForm() {
      const res = await addEmployee(this.form)
      console.log(res)
    }
  }
}
</script>

<style></style>
